package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.Group;
import com.cfets.annualAssessment.entity.GroupMember;
import com.cfets.annualAssessment.mapper.GroupMapper;
import com.cfets.annualAssessment.mapper.GroupMenMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by ShenYiChou on 2017/2/2.
 */
@Service
public class GroupService {
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private GroupMenMapper groupMenMapper;

    public List<Group> getGroupPage(Group group) {
        Page<?> page = PageHelper.startPage(group.getPage(), group.getRows());
        List<Group> groups=null;
        groups = GroupList(group);
        group.setTotalCount(page.getTotal());
        return groups;
    }

    public List<GroupMember> getGroupMemPage(Group group) {
        Page<?> page = PageHelper.startPage(group.getPage(), group.getRows());
        List<GroupMember> groupMembers=null;
        groupMembers = GroupMemberList(group);
        group.setTotalCount(page.getTotal());
        return groupMembers;
    }

    public boolean newGroup(Group group) {
        return groupMapper.insertSelective(group)>0;
    }

    public boolean newGroupMem(GroupMember groupMember) {
        return groupMenMapper.insertSelective(groupMember)>0;
    }

    public boolean updateGroup(Group group) {
        return groupMapper.updateByPrimaryKey(group)>0;
    }

    public boolean deleteGroupById(int id) {
        return groupMapper.deleteByPrimaryKey(id)>0;
    }

    public boolean deleteGroup(int id){
        boolean flag1 = true;
        List<GroupMember> groupMembers=null;
        Group group = groupMapper.selectByPrimaryKey(id);
        groupMembers = GroupMemberList(group);

        for(GroupMember item:groupMembers){
            int groupMemId = item.getId();
            boolean flag0 = (groupMenMapper.deleteByPrimaryKey(groupMemId)>0);
            if (!flag0) {
                flag1 = false;
            }
        }
        return flag1;
    }

    public boolean deleteGroupMember(String Group_name){
        Example example=new Example(Group.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(Group_name)){
            criteria.andEqualTo("group_name",Group_name);
        }
        return groupMenMapper.deleteByExample(example)>0;
    }

    public List<Group> GroupList(Group group){
        List<Group> groups=null;
        Example example=new Example(Group.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(group.getGroup_name())){
            criteria.andEqualTo("group_name",group.getGroup_name());
        }
        groups = groupMapper.selectByExample(example);
        return groups;
    }

//    public  List<GroupMember> GroupMenList(String Group_name){
//        Example example=new Example(Group.class);
//        Example.Criteria criteria =example.createCriteria();
//        if(!StringUtils.isEmpty(Group_name)){
//            criteria.andEqualTo("group_name",Group_name);
//        }
//        List<GroupMember> groupMembers = groupMenMapper.selectByExample(example);
//        return groupMembers;
//    }

    public List<GroupMember> GroupMemberList(Group group){
        List<GroupMember> groupMembers=null;
        Example example=new Example(GroupMember.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(group.getGroup_name())){
            criteria.andEqualTo("group_name",group.getGroup_name());
        }
        groupMembers = groupMenMapper.selectByExample(example);
        return groupMembers;
    }
}
